<template>
    <input-select :id="id" :styles="styles"  :sign="sign" :time="time"  :valsql="valsql" :index="index" isChange="Y"  :data="data"  @input="changeData" :exkey="exkey" @selectdata="selectdata"></input-select>
</template>
<script>
import InputSelect  from '../input/input-select'
export default {
    props:['id','styles','value','data','index','appid','valsql','sign','time'],
    name: "gb-select",
    components: {
        InputSelect
    },
     data(){
        return{
            options:[],
            lt_dat1:"",
            lt_dat2:"",
            exkey:{'label':'lt_ex3'},
            sfItemData:[],
            lg_ex1old:""
        }
    },
    created(){
         this.$post("bygex","byg/funeral/getFuneralSfItem",{}).then(data=>{
             this.sfItemData=[];
            for(let i=0;i<data.item.length;i++){
                this.sfItemData.push(data.item[i]);
            }
         });
        
    },
    watch:{
         data:{
            handler: function (newVal) {
                let ids=this.id.split(".");
                let pId=ids[0];
                let lt_dat1=this.data[pId]["lc_dat1"];
                let lt_dat2=this.data[pId]["lc_dat4"];
                let lc_ex1=this.data[pId]["lc_ex1"];
                
                let lg_ex1="";
                for(let i=0;i<this.options.length;i++){
                    let lg_ex1_=this.options[i].lg_ex1;
                    let optvalue=this.options[i].optvalue;
                    if(optvalue==lc_ex1){
                       lg_ex1=lg_ex1_;
                    }
                }
               
             
                if(lt_dat1!=this.lt_dat1||lt_dat2!=this.lt_dat2||this.lg_ex1old!=lg_ex1){
                  
                    this.lg_ex1old=lg_ex1
                    this.lt_dat1=lt_dat1;
                    this.lt_dat2=lt_dat2;
                    let days=1;
                    let hours=1;
                    if(lt_dat1&&lt_dat2){
                        
                        let lt_dat1_=lt_dat1.substring(0,10);
                       
                        
                        let lt_dat2_=lt_dat2.substring(0,10);
                        var stime =new Date(lt_dat1).getTime();
                        var etime = new Date(lt_dat2).getTime();
                        var usedTime = etime - stime;  //两个时间戳相差的毫秒数
                        hours=Math.floor(usedTime/(3600*1000));

                        let stime_ =new Date(lt_dat1_).getTime();
                        var etime_ = new Date(lt_dat2_).getTime();
                        var usedTime_ = etime_ - stime_;  //两个时间戳相差的毫秒数
                        days=Math.floor(usedTime_/(24*3600*1000));

                        
                        //this.$emit("input",data);
                        
                    }
                    let isOK=false;
                   // alert(days)
                     for(let i=0;i<this.data["item"].length;i++){
                        if(this.data["item"][i].szi_ex1==lg_ex1){
                            let row_=this.data["item"][i];
                            if(row_.szi_ex28=="自然天数"&&lt_dat1&&lt_dat2)
                                days++;
                             let lt_dat1_hour=lt_dat1.substring(11,13);
                            if(row_.szi_ex28=="12点后算半天"&&lt_dat1&&lt_dat2){
                              //  alert(lt_dat1_hour)
                                if(lt_dat1_hour>12){
                                    days=days+0.5;
                                }else{
                                    days++;
                                }
                            }
                            
                            let szi_ex2=Number(row_.szi_ex2);
                            row_.szi_ex4=days;
                            row_.szi_ex5=days*szi_ex2
                            this.$set(this.data["item"],i,row_);
                            isOK=true;
                            break;
                        }
                    }
                    if(!isOK){
                        for(let i=0;i<this.sfItemData.length;i++){
                            if(this.sfItemData[i].szi_ex1==lg_ex1){
                            
                                let row_=this.sfItemData[i];
                                if(row_.szi_ex28=="自然天数"&&lt_dat1&&lt_dat2)
                                    days++;
                                let lt_dat1_hour=lt_dat1.substring(11,13);
                                if(row_.szi_ex28=="12点后算半天"&&lt_dat1&&lt_dat2){
                                //  alert(lt_dat1_hour)
                                    if(lt_dat1_hour>12){
                                        days=days+0.5;
                                    }else{
                                        days++;
                                    }
                                }
                                
                                let szi_ex2=Number(row_.szi_ex2);
                                row_.szi_ex4=days;
                                row_.szi_ex5=days*szi_ex2
                                this.$set(this.data["item"],this.data["item"].length,row_);
                                break;
                            }
                        }
                    }
                    
                }
            },
            deep: true    //深度监听
         }
    },
    methods:{
        selectdata(data){
            this.options=JSON.parse(data)

             let lc_ex1=this.data["lc"]["lc_ex1"];
             for(let i=0;i<this.options.length;i++){
                let lg_ex1_=this.options[i].lg_ex1;
                let optvalue=this.options[i].optvalue;
                if(optvalue==lc_ex1){
                    this.lg_ex1old=lg_ex1_;
                }
            }
        },
    }
}
</script>